package com.threeox.commonlibrary.entity.engine.event;

import android.support.v4.app.Fragment;

import com.alibaba.fastjson.JSONObject;
import com.threeox.commonlibrary.entity.base.BaseObj;
import com.threeox.commonlibrary.util.FragmentUtils;
import com.threeox.commonlibrary.util.RegexHelper;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.res.StringHelper;

/**
 *
 * @ClassName: FragmentMsg
 *
 * @Description: TODO(Fragment 配置信息)
 *
 * @author 赵屈犇
 *
 * @date 创建时间: 2017/7/4 17:56
 *
 * @version 1.0
 *
 */
public class FragmentMsg extends BaseObj {

    private JSONObject params;// 传递参数
    private Integer requestCode;// requestCode
    private Class fragmentClass;// fragment 的Class
    private String fragmentClassName;// fragment 的Class全名称
    private String paramsModelName;// 请求参数的模型名称

    private JSONObject paramsKeys;// 传递参数替换的

    public void initData(StringHelper stringHelper) {
        try {
            RegexHelper.replaceJSONByRawName(paramsModelName, params);
            if (EmptyUtils.isNotEmpty(fragmentClassName)) {
                this.fragmentClassName = stringHelper.getStringText(fragmentClassName);
                this.fragmentClass = Class.forName(this.fragmentClassName);
            }
        } catch (Exception e) {
        }
        RegexHelper.replaceSys(params);
        this.paramsKeys = RegexHelper.getReplaceKeys(params);
    }

    public JSONObject getParams() {
        return params;
    }

    public void setParams(JSONObject params) {
        this.params = params;
    }

    public Integer getRequestCode() {
        return requestCode;
    }

    public void setRequestCode(Integer requestCode) {
        this.requestCode = requestCode;
    }

    public Class getFragmentClass() {
        return fragmentClass;
    }

    public void setFragmentClass(Class fragmentClass) {
        this.fragmentClass = fragmentClass;
    }

    public String getFragmentClassName() {
        return fragmentClassName;
    }

    public void setFragmentClassName(String fragmentClassName) {
        this.fragmentClassName = fragmentClassName;
    }

    public String getParamsModelName() {
        return paramsModelName;
    }

    public void setParamsModelName(String paramsModelName) {
        this.paramsModelName = paramsModelName;
    }

    /**
     * 初始化參數
     *
     * @param data
     * @return
     */
    public void initParam(Object data) {
        RegexHelper.replace(this.params, paramsKeys, data);
    }

    /**
     * 启动Fragment
     */
    public Fragment start() {
        if (null == getFragmentClass()) {
            return null;
        }
        return FragmentUtils.init(getFragmentClass()).putBundle(params).start();
    }

}
